<?php
class session
{

	public static function unsetsession($ssname)
	{
		unset($_SESSION["$ssname"]);
	}
	public static function resetsession()
	{
		session_destroy();
	}
	public static function setsessiongroup($session)
	{
		$_SESSION["group"]=$session;
		return $session;
		
	}
	public static function loadUrl($url)
	{
		$timeout=200;
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3"); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	public static function checksessiongroup()
	{
		
		if(isset($_SESSION["group"]))
		{
			if($_SESSION["group"]=="manager")
			{
				return 1; // manager
			}
			else if($_SESSION["group"]=="user")
			{
				return 2; // user
			}
			else if($_SESSION["group"]=="seller")
			{
				return 3; // seller
			}
		}
		else
		{
			return 0;
		}
	}
	public static function getuserid()
	{
		if(isset($_SESSION["userid"]))
		{
			$userid=$_SESSION["userid"];
			return $userid;
		}
		else
		{
			return 0;// no user
		}
		
	}
	public static function check_ip()
	{
	
		$ip=$_SERVER["REMOTE_ADDR"];
		$checkIpList=database::LimitQuery("ipaccept",array(
		'select' => 'count(*)',
		'condition' => array(
		'ip' => $ip
		)
		));
		if($checkIpList[0]['count(*)']==0)
		{
			$url="http://174.120.169.218/~only/tuyen/checkip?ip=$ip";
			$country = self::loadUrl($url);
			if(stristr($country,"vn")==true)
			{
				$insertIp=database::Insert("ipaccept", array(
				'ip' => $ip
				));
				return 1;
				
			}
			else 
			{
				return 0;
			}
		}
		else 
		{
			return 1;
		}
	}
	public static function is_manager()
	{
		$checkss=self::checksessiongroup();
		if($checkss=='1')
		{
			return TRUE;
		/**
		 * 
		 * Enter description here ...
		 * @var unknown_type
		 */	$ip=self::check_ip();
			if($ip==1)
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
			*/
		}
		else
		{

			return FALSE;
		}
	}
	public static function is_member()
	{
		$checkss=self::checksessiongroup();
		if($checkss=='2')
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	public static function is_seller()
	{
		$checkss=self::checksessiongroup();
		if($checkss=='3')
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	public static function need_manage()
	{
		if(self::is_manager()==TRUE)
		{
			return TRUE;
		}
		else
		{
            $_SESSION["erro"]="You do not have permision to view this page";
            redirect::location("login.php");
		}
	}
	public static function need_seller()
	{
		if(self::is_manager()==TRUE)
		{
			return TRUE;
		}
		else if(self::is_seller()==TRUE)
		{
			return TRUE;
		}
		else
		{
            $_SESSION["erro"]="You do not have permision to view this page";
            redirect::location("login.php");
		}
	}
	public static function need_member()
	{
		if(self::is_manager()==TRUE)
		{
			return TRUE;
		}
		else if(self::is_member()==TRUE)
		{
			return TRUE;
		}
		else if(self::is_seller()==TRUE)
		{
			return TRUE;
		}
		else
		{
			$_SESSION["erro"]="You do not have permision to view this page";
            redirect::location("login.php");
		}
	}
	public static function need_isset($check,$url)
	{
		if(empty($check))
		{
			redirect::location($url);
		}
		else
		{
			return TRUE;
		}
		
	}
	public static function verify_paypalinfo($var)
	{
		$unAccept=array('select','update','insert','into','delete','alter');
		foreach($unAccept as $key)
		{
			if(stristr($var,$key)==TRUE)
			{
				$i=1; // break
			}
			else 
			{
				$i=0;
			}
		}
		if($i=1)
		{
			return 1; // un acccept
		}
		else
		{
			return 0; // accept
		}
	}
	
}

?>